/**
 * 定义门禁管理模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.ws.wsclcManager.wsclcInfo.accessDoorManager', ['water.entity.accessDoor','water.entity.monitorStation']);
	module.controller('accessDoorManagerController',function($wdp,$rootScope,$scope,$accessDoorService,$monitorStationService){
		/**
		 * 新建对话框
		 */
		$scope.createDialog;
		/**
		 * 编辑对话框
		 */
		$scope.editDialog;
		/**
		 * 查询条件
		 */
		$scope.queryObject={};
		/**
		 * 门禁数据
		 */
		$scope.accessDoorData={};
		/**
         * 初始化站点数据
         */
		$scope.stationSelectList=null;
		/**
		 *新建站点数据
		 */
		$scope.stationSelecter;
		/**
		 * 编辑站点数据
		 */
		$scope.stationSelecterEdit;
		/**
		 * 新建数据
		 */
		$scope.dataAccessDoorCreate = {};
		/**
		 * 编辑数据
		 */
		$scope.dataAccessDoorEdit={};
		/**
		 * 新建按钮点击
		 */
		$scope.onCreateClick=function(){
			$scope.dataAccessDoorCreate = {};
			$scope.createDialog.open();
		};
		/**
		 * 新建提交
		 */
		$scope.onSubmitCreateClick = function(data){
			data.WWTPID=$wdp.viewValue.wwtpDetailParm.WWTPID;
			data.STATION_NAME=$scope.queryObject.STATION_NAME;
			$accessDoorService.createModel(data).then(function(){
				$scope._getAccessDoorList();
				$.messager.alert("保存成功","保存成功",'info');
				$scope.createDialog.close();
			});
		};
		/**
		 * 新建取消按钮
		 */
		$scope.onSubmitCancleClick = function(){
				$scope.createDialog.close();
		};
		/**
		 * 选择站点事件
		 */
		$scope.onStationChang=function(event){
			if(!event){
				
			}
			else{
				$scope.queryObject.STATION_NAME=event.v;
			}
		};
		/**
		 * 编辑按钮点击事件
		 */
		$scope.onEditClick = function(data){
			$scope.dataAccessDoorEdit = angular.copy(data);
			$scope.editDialog.open();
		}
		/**
		 * 提交编辑按钮点击事件
		 */
		$scope.onSubmitEditClick = function(data){
			data.WWTPID=$wdp.viewValue.wwtpDetailParm.WWTPID;
			data.STATION_NAME=$scope.queryObject.STATION_NAME;
			$accessDoorService.updateModel(data).then(function(){
				$scope._getAccessDoorList();
				$.messager.alert("保存成功","保存成功",'info');
				$scope.editDialog.close();
			});
		}
		/**
		 * 编辑取消事件
		 */
		$scope.onSubmitEditCancleClick = function(){
			$scope.editDialog.close();
	    };
	    /**
		 * 删除
		 */
		$scope.onSubmitDeleteClick=function(data){
			$.messager.confirm("确认","确定要删除这条记录吗？",function(r){
				if(r){
					$accessDoorService.deleteModel(data).then(function(){
						$scope._getAccessDoorList();
						$.messager.alert("删除成功","删除成功",'info');
					});			
				}
			});
		}
		/**
		 * 监控污水处理厂id变化
		 */
		if($wdp.viewValue.wwtpDetailParmWatch){
			$wdp.viewValue.wwtpDetailParmWatch();
		}
		$wdp.viewValue.wwtpDetailParmWatch = $rootScope.$watch(function(){return $wdp.viewValue.wwtpDetailParm;},function(){
			if(!$wdp.viewValue.wwtpDetailParm){
				return;
			}
			$scope.queryObject.WWTP_ID = $wdp.viewValue.wwtpDetailParm.WWTPID;
			$scope._getAccessDoorList();
			$scope._getStationSelectList();
        },true);
		/**
		 * 获取门禁数据
		 */
		$scope._getAccessDoorList=function(){
			$accessDoorService.getList($scope.queryObject).then(function(d){
				if(d.GridData.length < 1){
					$scope.accessDoorData = {};
					return;
				}
				$scope.accessDoorData=d.GridData;
			});
		};
		/**
		 * 获取站点列表，编辑或者新增的时候需要选择
		 */
		$scope._getStationSelectList = function(){
			$monitorStationService.getDomainList({WWTP_ID:$scope.queryObject.WWTP_ID}).then(function(d){
				$scope.stationSelectList = d;
				$scope.stationSelecter.setData(d);
				$scope.stationSelecterEdit.setData(d);
			});
		};
		
	});
}(window, angular));